package com.lmind.gps.t;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		
		// 创建线程池, 最少5个，最多10个线程
		ExecutorService service = new ThreadPoolExecutor(5, 10, 1, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
		// TODO Auto-generated method stub
		
		// 执行100个任务
		Future<Object> future = null;
		for (int i = 0; i < 100; i++) {
			future = service.submit(new Task("log " + i));
		}

		// 等待最后一个任务完成
		Object lastResult = future.get();
		System.out.println("last Result" + lastResult);
		
		service.shutdown();
	}
	
	static class Task implements Callable<Object> {
		private String log;
		public Task(String log) {
			this.log = log;
		}
		@Override
		public Object call() throws Exception {
			System.out.println(log);
			return "saved log: " + log;
		}
	}

}
